package com.sx.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.dfec.cache.redis.Cache;
import com.dfec.cache.redis.Redis;
import com.dfec.model.User;
import com.dfec.utils.Base64Coder;
import com.dfec.utils.DruidPlugin;
import com.dfec.utils.PropertiesUtil;
import com.dfec.utils.StringUtils;
import com.dfec.utils.Token;
import com.dfec.utils.ToolMD5;

public class UserLoginService   {
//	两种连接池切换
//	QueryRunner qr=new QueryRunner(DataSourceUtils.getDatasSource());
	QueryRunner qr=new QueryRunner(DruidPlugin.getDataSource());
	List<Object> list=null;

	//为用户生成token
	User  creatToken(User us){
		String token=Token.makeToken(64);
		us.setToken(token);
		return us;
	}
	
	//更新用户token
	void updateUserToken(User us) {
		String sql="update auther_user set token=? where phone=? and user_pass=?";
		Object[] params= {us.getToken(),us.getPhone(),us.getUser_pass()};
	  try {
		 qr.update(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	public boolean updateUser(User user) throws SQLException {
		// TODO Auto-generated method stub
		String sql="update auther_user set ";
		String base=sql;
		if(user.getHead_icon()!=null&&user.getHead_icon().length()>0)
		sql+="head_icon='"+user.getHead_icon()+"'";
		
		if(user.getEmail()!=null&&user.getEmail().length()>0)
		sql+="email='"+user.getEmail()+"'";
		
		if(user.getPhone()!=null&&user.getPhone().length()>0)
		sql+="phone='"+user.getPhone()+"'";
		
		if(user.getUser_name()!=null&&user.getUser_name().length()>0)
		sql+="user_name='"+user.getUser_name()+"'";
		
		if(user.getUser_pass()!=null&&user.getUser_pass().length()>0)
		sql+="user_pass='"+user.getUser_pass()+"'";
		
		if(user.getQq()!=null&&user.getQq().length()>0)
			sql+="qq='"+user.getQq()+"'";
		
		if(sql.equals(base)) {
			return false;
		}else {
			sql+=" where id="+user.getId();
		}
		
		return qr.update(sql)>0;
	}
	public List<Object> getUser(String phone) throws SQLException {
		list=new ArrayList<Object>();
		// TODO Auto-generated method stubprovince
		String sql ="select city,county,town,fax_number,name,work_unit,post,phone,other,ids from b_town_pre_info where phone='"+phone+"'";
		list.add(qr.query(sql, new MapListHandler()));
		return list;
	}
	public List<Object> getUser(String phone,String pass) throws SQLException {
		list=new ArrayList<Object>();
		// TODO Auto-generated method stubprovince
		if(StringUtils.isNotEmpty(pass)&&!pass.equals("000000")) {
			pass=ToolMD5.encodeMD5Hex(pass);
		}else {
			pass="";
		}
		String sql ="select city,county,town,fax_number,name,work_unit,post,phone,other,ids,token from b_town_pre_info where phone='"+phone+"' and pass='"+pass+"'";
		list.add(qr.query(sql, new MapListHandler()));
		
		return list;
	}

	public int rigesiter(String phone,String pass, String city, String cnty, String town, String fax_number, String name,
			String work_unit, String post, String other) throws SQLException {
		pass=ToolMD5.encodeMD5Hex(pass);
		String sql ="insert into b_town_pre_info(province,city,county,town,fax_number,name,work_unit,post,phone,other,pass) values(?,?,?,?,?,?,?,?,?,?,?)";
		Object[] params= {"陕西省",city,cnty,town,fax_number,name,work_unit,post,phone,other,pass};
	return qr.update(sql, params);
	}

	public int update(String phone, String city, String cnty, String town, String fax_number, String name,
			String work_unit, String post, String other,String ids) throws SQLException {
		String sql="update b_town_pre_info set ";
		String base=sql;
		
		if(StringUtils.isNotEmpty(phone))
		sql+="phone='"+phone+"'";
		
		if(StringUtils.isNotEmpty(city))
		sql+="city='"+city+"'";
		
		if(StringUtils.isNotEmpty(town))
		sql+="town='"+town+"'";
		
		if(StringUtils.isNotEmpty(fax_number))
		sql+="fax_number='"+fax_number+"'";
		
		if(StringUtils.isNotEmpty(name))
		sql+="name='"+name+"'";
		
		if(StringUtils.isNotEmpty(work_unit))
			sql+="work_unit='"+work_unit+"'";
		
		if(StringUtils.isNotEmpty(post))
			sql+="post='"+post+"'";
		
		if(StringUtils.isNotEmpty(other))
			sql+="other='"+other+"'";
		
		if(StringUtils.isNotEmpty(cnty))
			sql+="county='"+cnty+"'";
		
		if(sql.equals(base)) {
			return 0;
		}else {
			sql+=" where ids="+ids;
		}
		
		return qr.update(sql);
	}
	
	
	public int update(String ids,String pass,String oldpass) throws SQLException {
		String sql="update b_town_pre_info set ";
		String base=sql;
		
		if(StringUtils.isNotEmpty(pass))
			sql+="pass='"+ToolMD5.encodeMD5Hex(pass)+"'";
		if(sql.equals(base)) {
			return 0;
		}else {
			sql+=" where ids="+ids +" and pass='"+ToolMD5.encodeMD5Hex(oldpass)+"'";
		}
		
		return qr.update(sql);
	}
	
	public List<Object> getUserByIds(String ids,String pass) throws SQLException {
		list=new ArrayList<Object>();
		// TODO Auto-generated method stubprovince
		String sql ="select city,county,town,fax_number,name,work_unit,post,phone,other,ids from b_town_pre_info where ids='"+ids+"' and pass='"+ToolMD5.encodeMD5Hex(pass)+"'";
		list.add(qr.query(sql, new MapListHandler()));
		return list;
	}

	public int updateToken(String phone, String token)throws SQLException {
		// TODO Auto-generated method stub
		String sql ="update b_town_pre_info set token='"+token+"' where phone='"+phone+"'" ;
		return qr.update(sql);
	}
	
	
	/**
	 * 用户token验证 ，实现但设备单账户登录
	 * @param token
	 * @return
	 */
	public boolean getUserByToken(String token) {
		// TODO Auto-generated method stub
		String sql ="select phone from b_town_pre_info where token='"+token+"'" ;
		List<Map<String, Object>> lists;
		try {
			lists = qr.query(sql, new MapListHandler());
			if(lists.size()>0&&StringUtils.isNotEmpty(lists.get(0).get("phone").toString())) {
				if(PropertiesUtil.config_redis_use) {
					//设置缓存token
					Cache ca = Redis.use();
					ca.setex(lists.get(0).get("phone"),60, token);	
				}
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		return false;
	}
}
